package persistencia;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import modelo.Noticia;

public class NoticiaDAO {

    private Connection con;
    private PreparedStatement pstm;
    private ResultSet rs;

    public NoticiaDAO() throws SQLException {

        DriverManager.registerDriver(new org.postgresql.Driver());
        con = DriverManager.getConnection("jdbc:postgressql://localhost/teste","postgres","postgres");

    }

    public void Salvar(Noticia noticia) throws SQLException {
        pstm = con.prepareStatement("insert into noticia(titulo, texto, data, fonte) values (?, ?, ?, ?)");

        pstm.setString(1, noticia.getTitulo());
        pstm.setString(2, noticia.getTexto());
        pstm.setDate(3, (Date) noticia.getData());
        pstm.setString(4, noticia.getFonte());
        pstm.execute();
    }

    public void alterar(Noticia noticia) throws SQLException {
        pstm = con.prepareStatement("update noticia set titulo = ?, texto = ?, data = ?, fonte = ? where id = ?" );

        pstm.setString(1, noticia.getTitulo());
        pstm.setString(2, noticia.getTexto());
        pstm.setDate(3, (Date) noticia.getData());
        pstm.setString(4, noticia.getFonte());
        pstm.setInt(5, noticia.getId());
        pstm.execute();

    }

    public void Excluir(Noticia noticia) throws SQLException {
        pstm = con.prepareStatement("delete from noticia id = ?");
        pstm.setInt(1, noticia.getId());
        pstm.execute();
    }

    public Noticia pesquisar(int id) throws SQLException {
        Noticia noticia = null;
        pstm = con.prepareStatement("select * from noticia where id = ?");
        pstm.setInt(1, id);
        rs = pstm.executeQuery();
        if (rs.next()) {
            noticia = new Noticia();
            noticia.setTitulo(rs.getString("titulo"));
            noticia.setTexto(rs.getString("texto"));
            noticia.setData(rs.getDate("data"));
            noticia.setFonte(rs.getString("fonte"));
            noticia.setId(rs.getInt("id"));

        }
        return noticia;

    }

    public List<Noticia> getTodosNoticias() throws SQLException {
        ArrayList<Noticia> noticias = new ArrayList();
        Noticia noticia = null;
        
        pstm = con.prepareStatement("select * from noticia order by data");

        rs = pstm.executeQuery();
        while (rs.next()) {
            noticia = new Noticia();
            noticia.setTitulo(rs.getString("titulo"));
            noticia.setTexto(rs.getString("texto"));
            noticia.setData(rs.getDate("data"));
            noticia.setFonte(rs.getString("fonte"));
            noticia.setId(rs.getInt("id"));
            noticias.add(noticia);
        }

        return noticias;
    }

    

}
